package com.example.asyntaskdemo;


import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 加载图片
 * @author Administrator
 *
 */

public class MainActivity extends ListActivity {
	private static String[] titles = { "2013 CJ", "����˫��ԡ��ʪ���ջ�", "�ⵯ��Ů��Ϯ����",
			"2013 CJ2", "twins����˽����������" , 
			"twins����˽����������", 
			"twins����˽����������", 
			"twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"
			, "twins����˽����������"};
	private static String[] paths = {
			"http://bgimg1.meimei22.com/pic/bagua/2013-11-19/1/5562900520130806130014031_640.jpg",
			"http://imgs.xiuna.com/xiezhen/2012-8-3/2/13.jpg",
			"http://imgs.xiuna.com/xiezhen/2012-8-3/2/15.jpg",
			"http://bgimg1.meimei22.com/pic/bagua/2013-11-19/1/5562900520130806130030039_640.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg",
			"http://img.djys.com/meimo/2013-3-25/1/1358014538008586491.jpg"

	};
	private ImageAdapter adapter;
	private ImageView myPic;
	private Bitmap tempMap;
	private ProgressDialog progressDialog;
	Handler myHandler = null;
	Button button;
	Button setting;
	Thread thread;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myPic = (ImageView)findViewById(R.id.mypic);
		button = (Button)findViewById(R.id.button);
		setting = (Button)findViewById(R.id.button1);
		setting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(MainActivity.this, Setting.class));
			}
		});
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressDialog = new ProgressDialog(MainActivity.this);
				progressDialog.setTitle("����ͼƬ��");
				progressDialog.setCancelable(false);
				progressDialog.show();
				thread = new myThread();
				thread.start();
			}
		});

	}
	/**
	 * 	在主线程处理UI更新
	 * @author Administrator
	 *@version 1.0
	 */
	private class MyHandler extends Handler{

		public MyHandler(Looper looper) {
			super(looper);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Log.i("MyHandler", msg.obj.toString());
			progressDialog.dismiss();
			setListAdapter(adapter);
		}
		
	}

	/**
	 * 多线程中下载网页图片
	 * @author Administrator
	 *	
	 */
	private class myThread extends Thread{
		private class newHandler extends Handler{

			public newHandler(Looper looper) {
				super(looper);
				// TODO Auto-generated constructor stub
			}

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				Log.i("newHandler", msg.obj.toString());
			}
			
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			//Looper.prepare();
			
			Looper curLooper = Looper.myLooper();
			Looper mainLooper = Looper.getMainLooper();
			String msgString ;
			if(curLooper == null){
				myHandler = new MyHandler(mainLooper);
				msgString = "������Looper";
				Log.i("Thread", msgString);
			}else{
				myHandler = new newHandler(curLooper);
				msgString = "curLooper����Ϣ";
				Log.i("Thread", msgString);
			}
			myHandler.removeMessages(0);
			adapter = new ImageAdapter(getApplicationContext());
			List<MyImage> pictures = new ArrayList<MyImage>();
			for(int i = 0;i<paths.length;i++){
				MyImage pic = new MyImage();

					try {
						pic.setPicture(ImageService.getImage(paths[i]));
					} catch (Throwable e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				pictures.add(pic);
			}
			for(int i = 0 ; i<pictures.size();i++){
				adapter.add(new SimpleItem(titles[i], pictures.get(i)));
				
			}
			//new AddStringTask().execute(); 
			
			Message msg = myHandler.obtainMessage(1, 1, 1, msgString);
			Log.i("Thread", "Thread");
			myHandler.sendMessage(msg);
			//Looper.loop();
		}
		
	}

	private class SimpleItem {
		public String url;
		public MyImage pic;

		public SimpleItem(String url, MyImage pic) {
			this.url = url;
			this.pic = pic;
		}
	}

	public class ImageAdapter extends ArrayAdapter<SimpleItem> {
		public ImageAdapter(Context context) {
			super(context, 0);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.pic, null);
			}
			TextView showurl = (TextView) convertView
					.findViewById(R.id.showurl);
			showurl.setText(getItem(position).url);
			ImageView image = (ImageView) convertView.findViewById(R.id.pic);
			 //new DownLoad().execute(getItem(position).path);
				image.setImageBitmap(getItem(position).pic.getPicture());

			return convertView;
		}
	}
}
